<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/18 0018
 * Time: 15:30
 */

namespace Admin\Controller;

use Think\Controller;
class TopController extends Controller
{

    public function index(){
        $id=session('id');
        $adminmodel=M('admin');
        $admin=$adminmodel->where(array('id'=>$id))->select();
        $this->admin=$admin;
        $newsmodel=M('news');
        $news=$newsmodel->order('id ')->join('users on users.uid=news.n_uid')->select();
        $this->news=$news;
        for ($i = 0; $i < count($news); $i++){
            $news[$i]["code"] = $i + 1;

            if($news[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($news[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($news[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($news[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($news[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($news[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($news[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $news[$i]["tid"]=$type[$i];
        }

        if(!$id)
        {$this->error('登录已过期!',U("index.php/Admin/Index/index"));}
        elseif($news){
            $this->assign('list', $news);
            $this->display();
        }
        elseif ($admin){
            $this->assign('admin', $admin);
            $this->display();
        }
        else{ $this->error('获取失败!', U("Admin/news/index"));}


    }

    public function edittopa(){
        $id=(int)$_GET['id'];
        /*dump($id);
        exit();*/
        $d['top']=(int)!$_GET['top'];
        $news=M('news');
        $result= $news->where(array("id"=>$id))->save($d);
        if($result){
            $this->assign('list', $result);

            $this->success('修改成功');
        }
        else{
            $this->error('修改失败!');
        }

    }





    public function edittopb(){
        $id=(int)$_GET['id'];
        /* dump($id);
         exit();*/
        $d['top']=(int)!$_GET['top'];
        $news=M('news');
        $result= $news->where(array("id"=>$id))->save($d);
        if($result){
            $this->assign('list', $result);

            $this->success('修改成功');
        }
        else{
            $this->error('修改失败!');
        }

    }



    public function search(){
        $text = trim(I('post.text'));
        $newsmodel=M('news');
        $a['title']=array('like',"%{$text}%");
        /*$b['content']=array('like',"%{$text}%");
        $c['adduser']=array('like',"%{$text}%");*/
        $select=$newsmodel->where($a)->join('users on users.uid=news.uid')->select();
        for ($i = 0; $i < count($select); $i++){
            $select[$i]["code"] = $i + 1;
            if($select[$i]["tid"]== 1){
                $type[$i]='体育';
            }
            elseif($select[$i]["tid"]== 2)
            {
                $type[$i]='时政';

            }
            elseif($select[$i]["tid"]== 3)
            {
                $type[$i]='娱乐';

            }
            elseif($select[$i]["tid"]== 4)
            {
                $type[$i]='科技';

            }

            elseif($select[$i]["tid"]== 5)
            {
                $type[$i]='军事';

            }
            elseif($select[$i]["tid"]== 6)
            {
                $type[$i]='游戏';

            }
            elseif($select[$i]["tid"]== 7)
            {
                $type[$i]='视频';

            }
            $select[$i]["tid"]=$type[$i];
        }
        if($select)
        {
            $this->assign('list', $select);
            $this->display();
        }
        else{ $this->error('未查到相关结果,请重新查询',U("Top/index"));}
    }



}